#include <bits/stdc++.h>
// 2025/02/03
// tag: 
// Author: Zhang Muen
using namespace std;

int n, l[20001], r[20001], dp[20001][2];

signed main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> l[i] >> r[i];
    dp[1][0] = r[1] * 2 - l[1] - 1, dp[1][1] = r[1] - 1;
    for (int i = 2; i <= n; i++){
        dp[i][0] = min(abs(l[i - 1] - r[i]) + dp[i - 1][0], abs(r[i - 1] - r[i]) + dp[i - 1][1]);
        dp[i][1] = min(abs(r[i - 1] - l[i]) + dp[i - 1][1], abs(l[i - 1] - l[i]) + dp[i - 1][0]);
        dp[i][0] += r[i] - l[i] + 1, dp[i][1] += r[i] - l[i] + 1;
    }
    cout << min(dp[n][0] + n - l[n], dp[n][1] + n - r[n]);
    return 0;
}